/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo;

import Controlador.MotorPolicia;
import Controlador.PeritoColision;
import Vista.Pantalla;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

//import java.io.Serializable;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Canvas;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class Escenario extends Canvas implements WindowListener {
    
    //implements Serializable, WindowListener {

    private static final long serialVersionUID = 1L;

    private String mensaje;
    public JFrame jframe;
    public Vector figura = new Vector();

    //TODO:definir figuras en pantalla
    //TODO: usar listas para cada familia de figuras
    Pared pared1, pared2, pared3, pared4, pared5, pared6, pared7,pared8,pared9,pared10;
    Autito auto1,auto2;
    Base base;
    Destino destino;
    Policia policia1,policia2,policia3;
    MotorPolicia motorPoli1;
    public PeritoColision perito;
    
    int i, j;
    Boolean b;
    transient Image imgBuff;
    transient Graphics grafBuff;
    public JLabel jlabelCliente, jlabelServidor;

    public Escenario() {
    }

    public Escenario(String mensaje) {

        setMensaje(mensaje);
        jframe = new JFrame(mensaje);

        jframe.addWindowListener(this);

        //TODO: poner en falso cuando lo mandamos por socket
        jframe.setVisible(true);

        jframe.setLayout(null);
        jframe.setBounds(20, 20, 800, 600);
        jframe.setBackground(Color.gray);
        setBounds(40, 40, 800, 600);
        setBackground(Color.gray);
        jframe.getContentPane().add(this);

       // jlabelCliente = new JLabel();
       // jlabelCliente.setBounds(800, 50, 200, 200);
       // jframe.getContentPane().add(jlabelCliente);

       // jlabelServidor = new JLabel();
       // jlabelServidor.setBounds(800, 300, 200, 200);
       // jframe.getContentPane().add(jlabelServidor);


        pared1 = new Pared(1, 0, 0, 600, 10, Color.green, 1000, true);
        this.getFigura().add(pared1);
        pared2=new Pared(2, 0,0,10,800,Color.green,1000,true);
        this.getFigura().add(pared2);
        pared3=new Pared(3, 0,590,10,800,Color.green,1000,true);
        this.getFigura().add(pared3);
        pared4=new Pared(4, 790,0,600,10,Color.green,1000,true);
        this.getFigura().add(pared4);

        pared5=new Pared(5, 440,105,90,190,Color.yellow,1000,true);
        this.getFigura().add(pared5);
        pared6=new Pared(6, 140,415,90,190,Color.yellow,1000,true);
        this.getFigura().add(pared6);
        pared7=new Pared(7, 140,105,90,190,Color.yellow,1000,true);
        this.getFigura().add(pared7);
        pared8=new Pared(8, 440,415,90,190,Color.yellow,1000,true);
        this.getFigura().add(pared8);
        pared9=new Pared(9, 140,250,90,190,Color.yellow,1000,true);
        this.getFigura().add(pared9);
        pared10=new Pared(10, 440,250,90,190,Color.yellow,1000,true);
        this.getFigura().add(pared10);

        auto1=new Autito(1, 30,20,20,20,Color.ORANGE,5,true, this);
        this.getFigura().add(auto1);
        auto2=new Autito(2, 30,560,20,20,Color.blue,5,true, this);
        this.getFigura().add(auto2);

        base = new Base(0, 10,260,80,30,Color.MAGENTA,1,true);
        this.getFigura().add(base);

        destino = new Destino(0, 760,260,80,30,Color.WHITE,1,true);
        this.getFigura().add(destino);

        policia1 = new Policia(1, 320,15,30,30,Color.RED,1000,true, this);
        this.getFigura().add(policia1);
        policia2 = new Policia(2, 370,15,30,30,Color.RED,1000,true, this);
        this.getFigura().add(policia2);
        policia3 = new Policia(3, 420,15,30,30,Color.RED,1000,true, this);
        this.getFigura().add(policia3);

        motorPoli1 = new MotorPolicia(policia3);
        motorPoli1.start();

        
        perito = new PeritoColision(this.getFigura());
        //perito.start();
        
         
       
         
    }

    public void ejecutar() {

        System.out.println(getMensaje());
        this.repaint();
        jframe.setVisible(true);
    }
    
    
 

    public void refrescar() {

        this.repaint();
        //this.checkCollisions();
        
 //creo q hace falta para mandarlo por socket
//        jframe.setVisible(false);
    }

    public Vector<Figuras> getFigura() {

        return figura;
    }

    public void setFigura(Vector<Figuras> fig) {

        this.figura = fig;
    }

    public void setMensaje(String msj) {

        this.mensaje = msj;
    }

    public String getMensaje() {

        return mensaje;
    }

    
    @Override
    public void update(Graphics g) {

        paint(g);
    }

    @Override
    public void paint(Graphics g) {

        Class tiposParam[] = {Graphics.class};
        if (grafBuff == null) {

            imgBuff = createImage(jframe.getWidth(), jframe.getHeight());
            grafBuff = imgBuff.getGraphics();

        }
        for (i = (this.getFigura().size()) - 1; i >= 0; i--) {

            try {
                getClass().getMethod("estado" + Boolean.valueOf(getFigura().get(i).vida.equals(0)).
                        toString(), tiposParam).invoke(this, grafBuff);

            } catch (Exception e) {
            }
        }
        g.drawImage(imgBuff, 0, 0, this);
        this.grafBuff.clearRect(0, 0, jframe.getWidth(), jframe.getHeight());
    }

    public void estadotrue(Graphics g) {
    }

    public void estadofalse(Graphics g) {

        getFigura().get(i).dibujar(g);
    }



    public static void main(String[] args) {

       Escenario juego=new Escenario("SimTrafic");
       
       Pantalla pantalla = new Pantalla();
       pantalla.setEscenario(juego);
       pantalla.start();
    }
        public void windowClosing(WindowEvent e) {
        // throw new UnsupportedOperationException("Not supported yet.");
        System.exit(0);
    }

    public void windowClosed(WindowEvent e) {
        //  throw new UnsupportedOperationException("Not supported yet.");
        System.exit(0);
    }

    public void windowIconified(WindowEvent e) {
        //  throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeiconified(WindowEvent e) {
        //    throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowActivated(WindowEvent e) {
        //  throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeactivated(WindowEvent e) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowOpened(WindowEvent e) {
        try{
            throw new UnsupportedOperationException("Not supported yet.");
        }catch(Exception ex) {}
    }
}
